<template>
  <div class="content-block block-1">
    <div class="info-head">{{ title }}</div>
    <div class="info-content padding-bottom">
      <v-chart :option="chartOption" style="width: 100%;height:300px;" @click="onClick"></v-chart>
    </div>

    <custom-dialog
      ref="dialog" :title="modalTitle" width="540px" height="160px"
      show-cancel-button :show-confirm-button="false" cancel-button-text="关闭"
    >
      <el-table
        class="table" :data="lists"
        v-loading="loading"
        :header-cell-style="{
          backgroundColor: themeColor,
          color: 'white',
        }"
      >
        <el-table-column v-for="column in columns" :key="column.prop" v-bind="column"></el-table-column>
      </el-table>
    </custom-dialog>
  </div>
</template>

<script>
import CustomDialog from '@/components/custom-dialog'

export default {
  name: 'grid-item-one',
  components: { CustomDialog },
  props: {
    title: String,
    data: {
      type: Array,
      default () {
        return []
      }
    }
  },
  data () {
    return {
      loading: false,
      columns: [
        { label: '公司名称', prop: 'col1' },
        { label: '数据值', prop: 'col2' },
      ],
      lists: [],
      modalTitle: '',
    }
  },
  computed: {
    themeColor () {
      return this.$store.state.user.theme
    },
    months () {
      const data = this.data
      return data.map(v => v.name)
    },
    thisPeriods () {
      const data = this.data
      return data.map(v => v.usage.thisPeriod)
    },
    samePeriods () {
      const data = this.data
      return data.map(v => v.usage.samePeriod)
    },
    chartOption () {
      return {
        color: ['#50fdfc', '#e6b00e'],
        legend: {
          left: 'center',
          top: 'bottom',
          textStyle: {
            color: '#6b92b5',
          }
        },
        tooltip: {
          trigger: 'item'
        },
        grid: {
          left: '5%',
          right: '5%'
        },
        xAxis: {
          type: 'category',
          data: this.months,
          axisLine: {
            lineStyle: {
              color: '#2b7ed6',
            }
          },
          axisLabel: {
            color: '#6b92b5',
          },
        },
        yAxis: {
          type: 'value',
          name: '万吨',
          axisLine: {
            lineStyle: {
              color: '#2b7ed6',
            }
          },
          splitLine: {
            lineStyle: {
              color: '#024287',
            }
          },
          axisLabel: {
            color: '#6b92b5',
          },
        },
        series: [
          {
            type: 'bar',
            name: '本期',
            data: this.thisPeriods,
          },
          {
            type: 'bar',
            name: '同期',
            data: this.samePeriods,
          }
        ]
      }
    }
  },
  methods: {
    onClick (params) {
      const { data } = this
      const { dataIndex, name, value, seriesIndex, seriesName } = params
      const { usage } = data[dataIndex]

      console.log(usage, seriesIndex)
      this.modalTitle = name + seriesName + '数据详情'
      const max = Number(value)
      const rand = Math.round(Math.random() * max)
      this.lists = [
        { col1: '山东佰士达木业有限公司', col2: rand },
        { col1: '济宁阳光化学有限公司', col2: max - rand },
      ]
      this.open()
    },
    open () {
      this.$nextTick(() => {
        this.$refs.dialog.open()
      })
    },
  },
}
</script>

<style scoped>

</style>
